package addon.w3.mdx.com;

import java.io.IOException;

import addon.w3.mdx.MDXCOM;
import addon.w3.mdx.LEDataInputStream;
import addon.w3.mdx.MDXException;

/**
 * OBJ
	long	nbytesi;
	ASCII	Name;				(0x50 bytes)
	long	ObjectID;
	long	Parent;				(0:default;0xFFFFFFFF:none)
	long	Type;				// HELP:0;BONE:256;LITE:512;EVTS:1024;ATCH:2048;CLID:8192;
	(KGTR)					//   +1(DontInherit Translation) +16(BillboardedLockX)
	(KGRT)					//   +2(DontInherit Scaling)	 +32(BillboardedLockY)
	(KGSC)					//   +4(DontInherit Rotation)	 +64(BillboardedLockZ)
	(KATV)					//   +8(Billboarded)		 +128(CameraAnchored)
 * @author madjk
 * 
 */
public class MDXObject extends MDXCOM {
	int nbytesi = -1;
	public String name = null;
	public int objectID = -1;
	public int parent = -1;
	public int type = -1;
	
	public MDXScaling kgtr = null;
	public MDXRotation kgrt = null;
	public MDXScaling kgsc = null;
	public MDXAlpha katv = null;
	
	public String getTag() {
		return "OBJ";
	}
	
	public void _decode(LEDataInputStream ins) throws IOException, MDXException {
		nbytesi = ins.readInt();
		byte nameTemp[] = new byte[80];
		ins.read(nameTemp);
		name = new String(nameTemp);
		objectID = ins.readInt();
		parent = ins.readInt();
		type = ins.readInt();
		
		String tag = null;
		byte temp[] = new byte[4];
		ins.read(temp);
		tag = new String(temp);
		if(tag.equals("KGTR")) {
			kgtr = new MDXScaling("KGTR","KGTR:Translation");
			kgtr.decode(ins);
		} else ins.unread(temp);
		
		ins.read(temp);
		tag = new String(temp);
		if(tag.equals("KGRT")) {
			kgrt = new MDXRotation();
			kgrt.decode(ins);
		} else ins.unread(temp);
		
		ins.read(temp);
		tag = new String(temp);
		if(tag.equals("KGSC")) {
			kgsc = new MDXScaling();
			kgsc.decode(ins);
		} else ins.unread(temp);
		
		ins.read(temp);
		tag = new String(temp);
		if(tag.equals("KATV")) {
			katv = new MDXAlpha("KATV","KATV:Visibility");
			katv.decode(ins);
		} else ins.unread(temp);
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("TAG : "+getTag());
		sb.append("\n\tILEN : "+nbytesi);
		sb.append("\n\tNAME : "+name);
		sb.append("\n\tOBJECTID : "+objectID);
		sb.append("\n\tPARENT : "+parent);
		sb.append("\n\tTYPE : "+type);
		sb.append("\n\tKGTR : "+(kgtr == null ? false : kgtr.toString()));
		sb.append("\n\tKGRT : "+(kgrt == null ? false : kgrt.toString()));
		sb.append("\n\tKGSC : "+(kgsc == null ? false : kgsc.toString()));
		sb.append("\n\tKATV : "+(katv == null ? false : katv.toString()));
		return sb.toString();
	}

	@Override
	public boolean isImplicitTag() {
		// TODO Auto-generated method stub
		return true;
	}
}